package trace

import (
	"errors"
	"fmt"
	"github.com/spf13/pflag"
)

const TraceName = "CZC"

type Options struct {
	Name     string  `mapstructure:"name" json:"name"`
	Endpoint string  `mapstructure:"endpoint" json:"endpoint"`
	Sampler  float64 `mapstructure:"sampler" json:"sampler"`
	Batcher  string  `mapstructure:"batcher" json:"batcher"`
}

func NewTelemetryOptions() *Options {
	return &Options{
		Name:     "telemetry",
		Endpoint: "http://127.0.0.1:14268/api/traces",
		Sampler:  1.0,
		Batcher:  "jaeger",
	}
}

func (o *Options) AddFlags(fs *pflag.FlagSet) {
	fs.StringVar(&o.Name, "telemetry.name", o.Name, "telemetry name.")
	fs.StringVar(&o.Endpoint, "telemetry.endpoint", o.Endpoint, "telemetry endpoint.")
	fs.Float64Var(&o.Sampler, "telemetry.sampler", o.Sampler, "telemetry sampler.")
	fs.StringVar(&o.Batcher, "telemetry.batcher", o.Batcher, "telemetry batcher.")

}

func (o *Options) Validate() error {
	if o.Name == "" {
		return errors.New("name is empty")
	}
	if o.Endpoint == "" {
		return errors.New("endpoint is empty")
	}
	if o.Sampler == 0 {
		return errors.New("sampler is empty")
	}
	if o.Batcher == "" {
		return errors.New("batcher is empty")
	}
	if o.Batcher != kindJaeger && o.Batcher != kindZipkin {
		return errors.New(fmt.Sprintf("batcher must in [%s,%s]", kindJaeger, kindZipkin))
	}
	return nil
}
